package com.daikin.ktmustwrite.ui.fragment.project

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.daikin.ktmustwrite.R
import com.daikin.ktmustwrite.app.base.BaseFragment
import com.daikin.ktmustwrite.app.ext.*
import com.daikin.ktmustwrite.app.weight.recycleview.DefineLoadMoreView
import com.daikin.ktmustwrite.app.weight.recycleview.SpaceItemDecoration
import com.daikin.ktmustwrite.databinding.IncludeListBinding
import com.daikin.ktmustwrite.ui.adapter.AriticleAdapter
import com.daikin.ktmustwrite.viewmodel.request.RequestProjectViewModel
import com.daikin.ktmustwrite.viewmodel.state.ProjectViewModel
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.include_recyclerview.*

class ProjectChildFragment : BaseFragment<ProjectViewModel, IncludeListBinding>() {
    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    //recyclerview的底部加载view 因为在首页要动态改变他的颜色，所以加了他这个字段
    private lateinit var footView: DefineLoadMoreView

    //是否是最新项目
    private var isNew = false

    //改项目对应的id
    private var cid = 0

    //请求的ViewModel
    private val requestProjectViewModel: RequestProjectViewModel by viewModels()

    //适配器
    private val articleAdapter: AriticleAdapter by lazy { AriticleAdapter(arrayListOf()) }

    companion object {
        fun newInstance(cid: Int, isNew: Boolean): ProjectChildFragment {
            val args = Bundle()
            args.putInt("cid", cid)
            args.putBoolean("isNew", isNew)
            val fragmnet = ProjectChildFragment()
            fragmnet.arguments = args
            return fragmnet
        }
    }

    override fun layoutId(): Int {
        return R.layout.include_list
    }

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            isNew = it.getBoolean("isNew")
            cid = it.getInt("cid")
        }
        loadsir = loadServiceInit(swipeRefresh) {
            loadsir.showLoading()
            requestProjectViewModel.getProjectData(true, cid, isNew)
        }
        //初始化recycleview
        recyclerView.init(LinearLayoutManager(context), articleAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f)))

            footView = it.initFooter(SwipeRecyclerView.LoadMoreListener {
                //触发加载更多时请求数据
                requestProjectViewModel.getProjectData(false, cid, isNew)
            })
            //初始化FloatingActionButton
//            it.initFloatBtn(floatbtn)
        }

        swipeRefresh.init {
            requestProjectViewModel.getProjectData(true,cid,isNew)
        }

    }

    override fun createObserver() {
        requestProjectViewModel.projectDataState.observe(viewLifecycleOwner, Observer {
            //设值 新写了个拓展函数，搞死了这个恶心的重复代码
            loadListData(it, articleAdapter, loadsir, recyclerView, swipeRefresh)
        })
    }
    override fun lazyLoadData() {
//        loadsir.showLoading()
        requestProjectViewModel.getProjectData(true, cid, isNew)
    }

}